package com.rimi.controller;

import com.rimi.bean.*;
import com.rimi.commons.CommonResult;
import com.rimi.model.dto.AppointmentListDto;
import com.rimi.model.dto.MedicalrecordsListDto;
import com.rimi.model.dto.PetsListDto;
import com.rimi.model.vo.AddAppointmentVo;
import com.rimi.model.vo.AddDateByDutyVo;
import com.rimi.service.AppointmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 预约功能
 */
@Controller
@CrossOrigin
@Api(description = "预约功能",tags = "预约功能")
public class AppointmentController {

    @Autowired
    AppointmentService appointmentService;

    @GetMapping("user/appointment/userPets")
    @ResponseBody
    @ApiOperation("显示用户的宠物")
    public CommonResult<List<PetsListDto>> showPets(){

        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<Pets> userPets = appointmentService.findPetByUserId(user.getUserId());
        ArrayList<PetsListDto> dtos = new ArrayList<>();
        for (Pets pets : userPets){
            PetsListDto petsListDto = new PetsListDto();
            BeanUtils.copyProperties(pets, petsListDto);
            dtos.add(petsListDto);
        }
        return CommonResult.onSuccess(dtos);
    }

    @GetMapping("user/appointment/dutyDoctorByDate")
    @ResponseBody
    @ApiOperation("显示当天值班医生的信息(Date)")
    public CommonResult<List<Doctor>> showDocByDate(String beginDate, String endDate){

        List<Doctor> dutyDoctor = appointmentService.findDoctorByDate(beginDate,endDate);

        return CommonResult.onSuccess(dutyDoctor);
    }

    @GetMapping("user/appointment/dutyDoctorByWeek")
    @ResponseBody
    @ApiOperation("显示当天值班医生的信息(Week)")
    public CommonResult<List<Doctor>> showDocByWeek(String week){

        List<Doctor> dutyDoctor = appointmentService.findDoctorByWeek(week);
        return CommonResult.onSuccess(dutyDoctor);
    }


    @PostMapping("user/appointment/addAppointment")
    @ResponseBody
    @ApiOperation("添加挂号订单")
    public CommonResult add(@RequestBody AddAppointmentVo addAppointmentVo) {
        User user = (User)SecurityUtils.getSubject().getPrincipal();
        Appointment appointment = new Appointment();
        appointment.setUserId(user.getUserId());

        BeanUtils.copyProperties(addAppointmentVo,appointment);
        return appointmentService.addAppointment(appointment);
    }

    @GetMapping("user/appointment/showAppointment")
    @ResponseBody
    @ApiOperation("显示挂号订单信息")
    public CommonResult<List<AppointmentListDto>> showAppointment(){
        User user = (User)SecurityUtils.getSubject().getPrincipal();
        List<Appointment> allAppointmentByUser = appointmentService.findAppointmentByUserId(user.getUserId());
        ArrayList<AppointmentListDto> dtos = new ArrayList<>();
        for (Appointment appointment : allAppointmentByUser) {
            AppointmentListDto appointmentListDto = new AppointmentListDto();

            Doctor doctor = appointment.getDoctor();
            Pets pets = appointment.getPets();
            appointmentListDto.setDoctorNickname(doctor.getDoctorNickname());
            appointmentListDto.setPetNickname(pets.getPetNickname());

            BeanUtils.copyProperties(appointment, appointmentListDto);
            dtos.add(appointmentListDto);
        }
        return CommonResult.onSuccess(dtos);
    }

    @DeleteMapping("user/appointment/deleteByUser")
    @ResponseBody
    @ApiOperation("删除挂号订单（用户逻辑删除）")
    public CommonResult deleteByUser(String appointmentId) {
        return appointmentService.deleteAppointmentByUser(appointmentId);
    }

    @DeleteMapping("user/appointment/deleteByAdministrator")
    @ResponseBody
    @ApiOperation("删除挂号订单（管理员删除）")
    public CommonResult deleteByAdministrator(String appointmentId) {
        return appointmentService.deleteAppointmentByAdministrator(appointmentId);
    }


    @GetMapping("user/appointment/showMedicalrecords")
    @ResponseBody
    @ApiOperation("显示就诊记录信息")
    public CommonResult<List<MedicalrecordsListDto>> showMedicalrecords(){
        User user = (User)SecurityUtils.getSubject().getPrincipal();
        List<Appointment> allMedicalrecordsByUser = appointmentService.findMedicalrecordsByUserId(user.getUserId());
        ArrayList<MedicalrecordsListDto> dtos = new ArrayList<>();
        for (Appointment appointment : allMedicalrecordsByUser) {
            MedicalrecordsListDto medicalrecordsListDto = new MedicalrecordsListDto();

            Doctor doctor = appointment.getDoctor();
            Pets pets = appointment.getPets();
            Prescription prescription = appointment.getPrescription();

            medicalrecordsListDto.setDoctorNickname(doctor.getDoctorNickname());
            medicalrecordsListDto.setPrescriptionDate(prescription.getPrescriptionDate());
            medicalrecordsListDto.setDoctorDiagnosis(prescription.getDoctorDiagnosis());
            medicalrecordsListDto.setPetNickname(pets.getPetNickname());

            BeanUtils.copyProperties(appointment, medicalrecordsListDto);
            dtos.add(medicalrecordsListDto);
        }
        return CommonResult.onSuccess(dtos);
    }
}
